///////////////////////////////////////////////////////////////////////////////
///// Assignment 3: CPU Scheduler
///// By: Brady Robichaud (robicb82) & Patrick Filtz (filtzp43)
///// Date: 4/19/2013
///// File: Process.java
///// Description: This class deals with the creation of processes, the copying
/////	of processes, check the number of bursts, & check arrival time.
///////////////////////////////////////////////////////////////////////////////
package round2;
public class Process {
	int pID;
	int arrivalTime;
	int waitingTime;
	int turnAroundTime;
	int terminateTime;
	int curIndex;
	int[] bursts;
	int curRemTime;
	boolean burstReady;
	
	//Constructor for process
	public Process(){ }
	
	//Constructor for processes 
	public Process(int pID, int aT, int[] bursts){
		this.pID = pID;
		arrivalTime = aT;
		this.bursts = deepCopy(bursts);
		curRemTime = 0;
		terminateTime = -1;
		curIndex = 0;
		burstReady = true;
	}
	
	//Used to deep copy a processes
	private int[] deepCopy(int[] copyMe){
		int[] dc = new int[copyMe.length];
		for(int i = 0; i < copyMe.length; i++){
			dc[i] = copyMe[i];
		}
		return dc;
	}
	
	//Checks to see if there are more bursts
	public boolean hasMore(){
		boolean answer = true;
		if(curIndex >= bursts.length){
			answer = false;
		}
		return answer;
	}
	
	//Checks to see if the bursts have been all processed
	protected void burstDone(int time){
		if(curIndex >= bursts.length - 1){
			terminateTime = time +1;
			turnAroundTime = terminateTime - arrivalTime;
		}
	}
	
	//Checks the arrival time
	public boolean hasArrived(int time){
		return (arrivalTime <= time ? true : false);
	}	
	
	//Constructor for processes, used for copying processes 
	public Process copyMe(){
		Process p = new Process();
		p.arrivalTime = arrivalTime;
		p.burstReady = burstReady;
		p.bursts = deepCopy(bursts);
		p.curIndex = curIndex;
		p.curRemTime = curRemTime;
		p.pID = pID;
		p.terminateTime = terminateTime;
		p.turnAroundTime = turnAroundTime;
		p.waitingTime = waitingTime;
		return p;
	}
	
	//Gets the arrival times
	protected String getArrStr(int[] in){
		String out = in[0] + "";
		for(int i = 1; i < in.length; i++){
			out+= ", " + in[i];
		}
		return out;
	}
	
	//Converts pid, arrival times, and the arrival time array to a string to be printed
	public String toString(){
		String output = "PID: %d, arival: %d, burst array: %s";
		
		return String.format(output, pID, arrivalTime, getArrStr(bursts));
	}
}
